package MiddleWare;
use strict;
use warnings;
use Data::Dumper;
use Time::HiRes qw(gettimeofday);

sub new
{
	my $class = shift;
	my %args = @_;
	
	
	my $self =  bless {  }, $class;
	$self->_init(\%args);
	
	return $self;	
}

sub _init
{
	my $self = shift;
	my $args = shift;
	
	for my $class (@{$args->{classes}}){
		push @{$self->{classes}}, $class;
	}
}

sub handle
{
	my ($self, $request) = @_;

	for my $class (@{$self->{classes}}){
		eval "require $class";
		
		my $t0 = gettimeofday;
		eval {
			$class->middleware_handle($request);
		};
		$request->server->log->warn(Dumper($@)) if $@;
		$request->server->log->warn("inner middle invoke: class = $class, time = ".(gettimeofday - $t0));
	}
	
	return $request;
}
1;
